package top.suven.base.http.admin.order.service.impl;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.core.redis.RedisClusterServer;
import top.suven.base.core.redis.RedisConstants;
import top.suven.base.core.redis.RedisKeys;
import top.suven.base.http.admin.order.dao.OrderWebDao;
import top.suven.base.http.admin.order.model.Order;
import top.suven.base.http.admin.order.model.OrderUserFirstPayInfo;
import top.suven.base.http.admin.order.service.OrderWebService;
import top.suven.base.http.admin.order.vo.response.OrderResponseVo;
import top.suven.base.http.admin.order.vo.response.OrderStatisticsVo;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.JsonUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Title: OrderWebServiceImpl.java
 * @Description: 订单的业务处理服务类
 * @author lixiangling
 * @date   2018/08/28 16:02:10
 * @version V1.0.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 *
 */
@Component("orderWebService")
public class OrderWebServiceImpl implements OrderWebService {
    private final static Logger logger = LoggerFactory.getLogger(OrderWebServiceImpl.class);
    @Autowired
    private RedisClusterServer redisClusterServer;
    @Autowired
    private OrderWebDao orderWebDao;
    /**
     * @Title: 获取订单分页信息
     * @Description:
     * @param order @{Link Order}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public PageData<OrderResponseVo> getOrderPage(Order order, int start, int pageSize){
        int count = orderWebDao.countEntity(order);
        if(count == 0){
            return new PageData<OrderResponseVo>(Collections.EMPTY_LIST,count);
        }
        List<Order> orderList = orderWebDao.findPageEntity(order, start, pageSize);
        if(orderList == null || orderList.isEmpty()){
            return new PageData<OrderResponseVo>(Collections.EMPTY_LIST,count);
        }
        List<OrderResponseVo> orderResponseList = orderList.stream().map(orderResponse -> {
            OrderResponseVo orderResponseVo = new OrderResponseVo();
            BeanUtil.copyProperties(orderResponse, orderResponseVo);
            return orderResponseVo;
        }).collect(Collectors.toList());
        return new PageData<OrderResponseVo>(orderResponseList,count);
    }
    /**
     * @Title: 获取订单列表信息
     * @Description:
     * @param order @{Link Order}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public List<Order> getOrderList(Order order){
        return orderWebDao.findEntity(order);
    }

    /**
     * @Title: 新增订单信息
     * @Description:
     * @param order @{Link Order}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public void addOrder(Order order){
        orderWebDao.createEntity(order);
    }
    /**
    * @Title: 修改订单信息
    * @Description:
    * @param order @{Link Order}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void updOrder(Order order){
        orderWebDao.updateEntity(order);
    }
   /**
    * @Title: 获取订单信息
    * @Description:
    * @param id @{Link String}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public Order getOrder(String id){
        Order order = new Order();
        order.setOrderId(id);
        return orderWebDao.retrieveEntity(order);
    }
    /**
     * @Title: 删除订单信息
     * @Description:
     * @param ids @{Link String[]}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public void del(String[] ids){
        orderWebDao.del(Arrays.asList(ids));
    }
   /**
    * @Title: 启用订单信息
    * @Description:
    * @param ids @{Link String[]}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void turnOn(String[] ids){
        orderWebDao.updateEnableStatus(EnableStatusEnum.ENABLE.getIndexStr(), Arrays.asList(ids));
    }
   /**
    * @Title: 禁用订单信息
    * @Description:
    * @param ids @{Link String[]}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void turnOff(String[] ids){
        orderWebDao.updateEnableStatus(EnableStatusEnum.DISABLE.getIndexStr(), Arrays.asList(ids));
    }

    @Override
    public OrderStatisticsVo getTwoDayPaidUserConversion() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String now = format.format(new Date());
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        String orderInfo = redisClusterServer.get(String.format("%s%s", RedisKeys.ORDER_STATISTIC_INFO, now));
        if(StringUtils.isNotBlank(orderInfo)){
            return JsonUtils.readValue(orderInfo, OrderStatisticsVo.class);
        }
        List<OrderUserFirstPayInfo> orderUserFirstPayInfos = orderWebDao.getTwoDayPaidUserConversion();
        if(orderUserFirstPayInfos == null || orderUserFirstPayInfos.isEmpty()){
            return orderStatisticsVo;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,-2);
        String beforeYesterDay = format.format(calendar.getTime());
        calendar.add(Calendar.DATE,1);
        String yesterday = format.format(calendar.getTime());

        for(OrderUserFirstPayInfo orderUserFirstPayInfo : orderUserFirstPayInfos){
            if(beforeYesterDay.equals(orderUserFirstPayInfo.getOrderTime())){
                orderStatisticsVo.setBeforeYesterdayCount(orderUserFirstPayInfo.getCount());
            }else if(yesterday.equals(orderUserFirstPayInfo.getOrderTime())){
                orderStatisticsVo.setYesterdayCount(orderUserFirstPayInfo.getCount());
            }
        }
        DecimalFormat numberFormat = new DecimalFormat("##.##");
        //安卓环比
        orderStatisticsVo.setRateOfRise(Float.parseFloat(numberFormat.format((orderStatisticsVo.getYesterdayCount()-orderStatisticsVo.getBeforeYesterdayCount())/
                (orderStatisticsVo.getBeforeYesterdayCount()==0?1f:orderStatisticsVo.getBeforeYesterdayCount()))));
        redisClusterServer.setex(String.format("%s%s", RedisKeys.ORDER_STATISTIC_INFO, now),
                JsonUtils.toJsonString(orderStatisticsVo), RedisConstants.ONE_DAY);
        return orderStatisticsVo;
    }
}